import { Component, OnInit ,Input } from '@angular/core';
import { trigger,state, style,transition,animate} from '@angular/animations';
import { Router, ActivatedRoute } from "@angular/router"; //路由引入
import { JsonApiService } from "app/core/api/json-api.service";
import {config} from '../../../shared/smartadmin.config';

declare var $: any;
@Component({
  	selector: 'question-record',
  	templateUrl: './question-record.component.html',
  	styleUrls:['./question-record.css'],
})
export class QuestionRecordComponent implements OnInit{
	@Input() isGetRecord: any;//接收父组件的值
	@Input() recordTime: any;//接收父组件的值
	isActivated:boolean;
  	isAddRecord:boolean=false;
  	recordArr=[];
  	sortType:string = '';
  	abnormalType:string = '';
  	describe:string='';
  	startTime:string='';
  	endTime:string='';
  	
  	totalCount:number=0;
  	pageSize:number=5;
  	curPage:number=1;
  	getTimeInput='';
  	repairedGuide:string='';
  	isPush='0';
  	sortTypeList:[{
  		value:"1",
  		label:"硬件"
  		children:[{
  			value:"11",
  			label:"传感器",
  			children:[{
  				value:"111",
  				label:"传感器直线",
  			},{
  				value:"112",
  				label:"传感器乱跳",
  			},{
  				value:"113",
  				label:"传感器极值",
  			},{
  				value:"114",
  				label:"传感器超过极值",
  			},{
  				value:"115",
  				label:"传感器没有通道信息"
  			}]
  		},{
  			value:"12",
  			label:"GPS",
  			children:[{
  				value:"121",
  				label:"gps漂移",
  			},{
  				value:"122",
  				label:"gps丢失"
  			}]
  		}]
	},{
  		value:"2",
  		label:"软件"
  		children:[{
  			value:"21",
			label:"上货异常",
		},{
			value:"22",
			label:"未识别上货",
		},{
			value:"23",
			label:"卸货异常",
		},{
			value:"24",
			label:"断电未归零"
		},{
			value:"25",
			label:"途中掉重量"
  		}]	
  	}];
  	faultItemlist:any = [{
  		type:'1',
  		phenomenon:'1',
  		typeText:'',
  		phenText:''
	}];	//故障现象明细数据数组
  	sensorType:any = [{
  		'type':1,
  		'text':'传感器1'
  	},{
  		'type':2,
  		'text':'传感器2'
  	},{
  		'type':3,
  		'text':'传感器3'
  	},{
  		'type':4,
  		'text':'传感器4'
  	},{
  		'type':5,
  		'text':'传感器5'
  	},{
  		'type':6,
  		'text':'传感器6'
  	},{
  		'type':7,
  		'text':'传感器7'
  	},{
  		'type':8,
  		'text':'传感器8'
  	},{
  		'type':9,
  		'text':'传感器9'
  	},{
  		'type':10,
  		'text':'传感器10'
	},{
  		'type':11,
  		'text':'传感器11'
  	},{
  		'type':12,
  		'text':'传感器12'
  	},{
  		'type':13,
  		'text':'传感器13'
  	},{
  		'type':14,
  		'text':'传感器14'
  	},{
  		'type':15,
  		'text':'传感器15'
  	},{
  		'type':16,
  		'text':'传感器16'
	}];
  	sensorPhen:any=[{
  		'type':1,
  		'text':'平直'
	},{
		'type':2,
		'text':'正最大值'
	},{
		'type':3,
		'text':'负最大值'
	},{
		'type':4,
		'text':'乱跳'
	},{
		'type':5,
		'text':'零值'
	}];
	GpsArr=[{'type':1,'text':'GPS坐标'}];
	GpsPhen:any=[{
		'type':1,
		'text':'丢失'
	},{
		'type':2,
		'text':'漂移'
	}];
	phenTypeArr=[];			//故障现象类型选项         例:传感器1  
	phenValueArr=[];		//故障现象选项值    例:最大正值
  	constructor(private jsonApiService: JsonApiService,) {}

  	ngOnInit(){}
  	//问题分类回调
  	sortTypeChange(){
  		this.abnormalType = '';
  		this.describe = '';
  		this.repairedGuide='';
  		this.isPush='0';
  	}
  	//硬件分类change事件回调
  	abnormalTypeChange(){
  		this.faultTypeChange();
  		this.describe = '';
  		this.repairedGuide='';
  		this.isPush='0';
  	}
	//删除故障现象事件
	item_del(sort){
		this.faultItemlist.splice(sort,1);
	}
	//添加故障现象事件
	item_add(){
//		var typeStr=this.phenTypeArr[0].text;     //默认添加第一个选项
//		var phenStr=this.phenValueArr[0].text;
//		var item={type:'1',phenomenon:'1',typeText:typeStr,phenText:phenStr};
		var item={type:'1',phenomenon:'1',typeText:'',phenText:''};
		this.faultItemlist.push(item);
	}
	faultTypeChange(){
		this.faultItemlist = [{type:'1',phenomenon:'1',typeText:'',phenText:''}];
		if(this.abnormalType === '传感器'){
			this.phenTypeArr = this.sensorType;
			this.phenValueArr = this.sensorPhen;
		}
//		if(this.abnormalType === 'GPS'){
//			this.phenTypeArr = this.GpsArr;
//			this.phenValueArr = this.GpsPhen;
//		}
	}
	selectedChange(el,type,params){
		var index=Number($(el.target).val())-1;
		if(type=='type'){
			params.type=(index+1)+'';
			params.typeText=this.phenTypeArr[index].text;
		}
		if(type=='phenomenon'){
			params.phenomenon=(index+1)+'';
			params.phenText=this.phenValueArr[index].text;
		}
	}
  	load(){
      	let data = {
			pageSize:this.pageSize,
			nowPage:this.curPage,
			parameters:{
				companyId:this.isGetRecord.companyId,
				carNumber:this.isGetRecord.carNumber,
				startDate:this.isGetRecord.startDate,
				endDate:this.isGetRecord.endDate,
			}
		}
		let reqdata = 'gridPager='+JSON.stringify(data);
		this.jsonApiService.http("dataExceptionRecord/dataRecordPage.html", reqdata).subscribe(result => {
			if(!result.isSuccess){
				this.jsonApiService.message(result.message);
				return;
			}
			this.recordArr = result.exhibitDatas;
			this.totalCount = result.recordCount;
		})
  	}
	//分页回调
	paginate(e){
		this.curPage = e.page+1;
		this.load();
	}
  	ngOnChanges() {
        if(this.isGetRecord.status){
        	this.load();
        }else{
        	this.recordArr=[];
        	this.totalCount=0;
        }
        if(this.getTimeInput!==''){
			this[this.getTimeInput]=this.recordTime;
        }
    }
  	onToggle(){
  		this.isActivated = !this.isActivated;
  		this.isAddRecord=false;
  	}
    toAddRecord(){
    	this.isAddRecord=true;
    }
	getFocusInput(val){
  		this.getTimeInput=val;
  	}
	//提交到设备故障记录
	submitDevice(){
		if(!this.isGetRecord.deviceId){
  			this.jsonApiService.message('先选择车辆...');
  			return;
  		}
		if(!this.sortType){
  			this.jsonApiService.message('请选择问题分类');
  			return;
  		}
  		if(!this.abnormalType){
  			this.jsonApiService.message('请选择问题分类');
  			return;
  		}
  		if(this.abnormalType != '传感器'){
  			if(!this.describe){
	  			this.jsonApiService.message('请选择问题描述');
	  			return;
	  		}
  		}
  		if(!this.startTime){
  			this.jsonApiService.message('起始时间不允许为空...');
  			return;
  		}
  		let faultPhenStr:any;
  		if(this.abnormalType === '传感器'){
  			faultPhenStr=[];
	  		if(this.sortType === '2'&&this.abnormalType){
				for(let item of this.faultItemlist){
					if(item.typeText&&item.phenText){
						faultPhenStr.push(common.stringTrim(item.typeText+'-'+item.phenText))
					}
				}
				if(!faultPhenStr||faultPhenStr.length<1){
		  			this.jsonApiService.message('请选择问题描述');
		  			return;
		  		}
	  		}
  		}else if(this.abnormalType === 'GPS'){
  			faultPhenStr = 'GPS坐标-'+this.describe;
  		}else if(this.abnormalType === '主机'){
  			faultPhenStr = '主机-'+this.describe;
  		}else if(this.abnormalType === '线材'){
  			faultPhenStr = '线材-'+this.describe;
  		}
  		
		let submitObj={
			deviceId:this.isGetRecord.deviceId,             //编号
			carNumber:this.isGetRecord.carNumber,			//车牌
			faultTypeName:this.abnormalType,				//故障类型
			faultTime:this.startTime,                   	//故障时间
			faultPhenomenon:faultPhenStr?faultPhenStr:'',	//故障现象:例  传感器1-平直
			isPush:this.isPush,								//是否推送
			repairedGuide:this.repairedGuide,				//维修指导
			repairedRecords:'',								//维修记录
			repairedStatus:'',				                //维修状态(故障状态)
			repairedTime:'',								//维修时间
			repairedUser:'',								//维修人员
			remark:'',										//备注
			repairedConclusion:'',							//维修总结
		}
		let reqdata = '';
		for(var item in submitObj){
			if(reqdata==''){
				reqdata=item+'='+submitObj[item];
			}else{
				reqdata =reqdata+ '&'+item+'='+submitObj[item];
			}
		}
		this.jsonApiService.http('deviceRepaired/validateRecord.html', 'deviceId='+this.isGetRecord.deviceId).subscribe(result => {
			if(result.valid){
				this.jsonApiService.confirm_message(result.message+'确认要继续提交?').then(()=>{
					this.jsonApiService.http('deviceRepaired/insertDeviceRepaired.html', reqdata).subscribe(result => {
						this.jsonApiService.message(result.message)
						if(result.success){
							this.load();
						}
					})
				})
			}else{
				this.jsonApiService.http('deviceRepaired/insertDeviceRepaired.html', reqdata).subscribe(result => {
					this.jsonApiService.message(result.message)
					if(result.success){
						this.load();
					}
				})
			}
		})
	}
	//问题提交
  	submitRecord(){
  		if(!this.isGetRecord.deviceId){
  			this.jsonApiService.message('先选择车辆...');
  			return;
  		}
  		if(!this.sortType){
  			this.jsonApiService.message('请选择问题分类');
  			return;
  		}
  		if(this.abnormalType != '传感器'){
  			if(!this.describe){
	  			this.jsonApiService.message('请选择问题描述');
	  			return;
	  		}
  		}
  		if(!this.startTime||!this.startTime){
  			this.jsonApiService.message('起始时间不允许为空...');
  			return;
  		}
  		if(!common.checkDateFormat(this.startTime)){
  			this.jsonApiService.message('开始时间格式不对,例:2018-05-20 00:00:01');
  			return;
  		}
  		if(!common.checkDateFormat(this.endTime)){
  			this.jsonApiService.message('结束时间格式不对,例:2018-05-20 00:00:01');
  			return;
  		}
  		let timeCheck=common.correctTimeParams(this.startTime,this.endTime,0);
		if(!timeCheck.status){
			this.jsonApiService.message(timeCheck.message);
			return;
		}
		let temStr:any = '';
		if(this.abnormalType === '传感器'){
  			let faultPhenStr=[];
	  		if(this.sortType === '2'&&this.abnormalType){
				for(let item of this.faultItemlist){
					if(item.typeText&&item.phenText){
						faultPhenStr.push(common.stringTrim(item.typeText+'-'+item.phenText));
					}
				}
	  		}
	  		temStr = faultPhenStr.join(',');
	  		if(!temStr){
	  			this.jsonApiService.message('请选择问题描述');
	  			return;
	  		}
  		}else if(this.abnormalType === 'GPS'){
  			temStr = 'GPS坐标-'+this.describe;
  		}else if(this.abnormalType === '主机'){
  			temStr = '主机-'+this.describe;
  		}else if(this.abnormalType === '线材'){
  			temStr = '线材-'+this.describe;
  		}else if(this.sortType === '1'){
  			temStr = this.describe;
  		}
  		var temVersion = '';
		this.jsonApiService.http("realtime/getRealTimeData.html", 'deviceId='+this.isGetRecord.deviceId).subscribe(result => {
			temVersion = result.data.version;
			let reqdata='deviceId='+this.isGetRecord.deviceId+'&version='+temVersion+'&startDate='+this.startTime+
	      	'&endDate='+this.endTime+'&reason='+temStr+'&exceptionType='+this.sortType+'&repairedGuide='+this.repairedGuide;
			this.jsonApiService.http("dataExceptionRecord/addDataException.html", reqdata).subscribe(result => {
				this.jsonApiService.message(result.message);
				if(result.success){
					this.load();
				}
			})
		})
  	}
}